﻿using System;
using System.Globalization;
using System.Threading;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Sinbiota.Web.Initialization.StructureMap;
using StructureMap;

namespace Sinbiota.Web
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

            routes.IgnoreRoute("robots.txt");
            routes.IgnoreRoute("sitemap");
            routes.IgnoreRoute("sitemap.gz");
            routes.IgnoreRoute("sitemap.xml");
            routes.IgnoreRoute("sitemap.xml.gz");
            routes.IgnoreRoute("google_sitemap.xml");
            routes.IgnoreRoute("google_sitemap.xml.gz");
            routes.IgnoreRoute("favicon.ico");

            
            log4net.Config.XmlConfigurator.Configure();
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterRoutes(RouteTable.Routes);

            // IoC Container: StructureMap:
            Bootstrapper.Bootstrap();
            ControllerBuilder.Current.SetControllerFactory(new StructureMapControllerFactory());

        }
        protected void Application_EndRequest()
        {
            // Make sure to dispose of NHibernate session if created on this web request
            ObjectFactory.ReleaseAndDisposeAllHttpScopedObjects();
        }

        // Localization: Application_AcquireRequestState is called for every request;
        protected void Application_AcquireRequestState(object sender, EventArgs e)
        {
            //It's important to check whether session object is ready
            if (HttpContext.Current.Session == null) return;
            // It is ready, get culture:
            var ci = (CultureInfo)Session["Culture"];
            //Checking first if there is no value in session 
            //and set default language 
            //this can happen for first user's request
            if (ci == null)
            {
                //Sets default culture to portuguese 
                var langName = "pt";
                //Try to get values from Accept lang HTTP header
                if (HttpContext.Current.Request.UserLanguages != null && HttpContext.Current.Request.UserLanguages.Length != 0)
                {
                    //Gets accepted list 
                    langName = HttpContext.Current.Request.UserLanguages[0].Substring(0, 2);
                }
                ci = new CultureInfo(langName);
                Session["Culture"] = ci;
            }
            //Finally setting culture for each request
            Thread.CurrentThread.CurrentUICulture = ci;
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name);
        }
    }
}